<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class LogJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * @var Model
     * @author luffyzhao@vip.126.com
     */
    private $model;
    /**
     * @var array
     * @author luffyzhao@vip.126.com
     */
    private $extra;

    /**
     * Create a new job instance.
     *
     * @param $model
     * @param array $extra
     */
    public function __construct($model, array $extra)
    {
        $this->model = $model;
        $this->extra = $extra;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(method_exists($this->model, 'logs') && $this->model->logs() instanceof HasMany){
            $this->model->logs()->create($this->extra);
        }
    }
}
